package com.tanhua.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.domain.AnalysisByDay;
import com.tanhua.domain.AnalysisChartDataItem;
import com.tanhua.mapper.AnalysisMapper;
import com.tanhua.utils.ComputeUtil;
import com.tanhua.vo.AnalysisChartDataVO;
import com.tanhua.vo.AnalysisSummaryVO;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AnalysisService extends ServiceImpl<AnalysisMapper, AnalysisByDay> {

    /**
     * 概要统计信息
     *
     * @return
     */
    public ResponseEntity summary() {
        AnalysisSummaryVO vo = new AnalysisSummaryVO();

        //cumulativeUsers   累计用户
        Long cumulativeUsers = this.baseMapper.queryCumulativeUsers();
        vo.setCumulativeUsers(cumulativeUsers);

        //activePassMonth   过去30天活跃用户
        Date now = new Date();
        String nowStr = new DateTime(now).toString("yyyy-MM-dd");
        String date30 = ComputeUtil.offsetDay(now, -30);
        Long activePassMonth = this.baseMapper.queryActiveUsers(date30, nowStr);
        vo.setActivePassMonth(activePassMonth);

        //activePassWeek    过去7天活跃用户
        String date7 = ComputeUtil.offsetDay(now, -7);
        Long activePassWeek = this.baseMapper.queryActiveUsers(date7, nowStr);
        vo.setActivePassWeek(activePassWeek);

        //newUsersToday     今日新增用户
        AnalysisByDay analysis = query().eq("record_date", nowStr).one();
        Integer newUsersToday = analysis.getNumRegistered();
        vo.setNewUsersToday(newUsersToday.longValue());

        //newUsersTodayRate 今日新增用户涨跌率
        String date1 = ComputeUtil.offsetDay(now, -1);
        AnalysisByDay outAnalysis = query().eq("record_date", date1).one();
        BigDecimal newUsersTodayRate = ComputeUtil.computeRate(newUsersToday.longValue(), outAnalysis.getNumRegistered().longValue());
        vo.setNewUsersTodayRate(newUsersTodayRate);

        //loginTimesToday   今日登录次数
        Integer loginTimesToday = analysis.getNumLogin();
        vo.setLoginTimesToday(loginTimesToday.longValue());

        //loginTimesTodayRate   今日登录次数涨跌率
        BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(loginTimesToday.longValue(), outAnalysis.getNumLogin().longValue());
        vo.setLoginTimesTodayRate(loginTimesTodayRate);

        //activeUsersToday  今日活跃用户
        Integer activeUsersToday = analysis.getNumActive();
        vo.setActiveUsersToday(activeUsersToday.longValue());

        //activeUsersTodayRate  今日活跃用户
        BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(activeUsersToday.longValue(), outAnalysis.getNumActive().longValue());
        vo.setActiveUsersTodayRate(activeUsersTodayRate);
        // 涨跌率
        return ResponseEntity.ok(vo);
    }

    /**
     * 新增、活跃用户、次日留存率
     *
     * @param sd   起始时间
     * @param ed   结束时间
     * @param type 101 新增、102 活跃用户、103 次日留存率
     * @return
     */
    public ResponseEntity usersChartData(Long sd, Long ed, Integer type) {
        AnalysisChartDataVO vo = new AnalysisChartDataVO();
        // 今年的数据
        // 准备查询的日期范围
        String startDate = DateUtil.date(sd).toString("yyyy-MM-dd");
        String endDate = DateUtil.date(ed).toString("yyyy-MM-dd");
        //从数据库里查询日期范围内的数据
        List<AnalysisByDay> analysisByDayList = query().between("record_date", startDate, endDate).list();
//        把每天的数据转换成AnalysisChartDataItem对象
        List<AnalysisChartDataItem> thisYearLsit = new ArrayList<>();
        for (AnalysisByDay analysisByDay : analysisByDayList) {
            AnalysisChartDataItem item = new AnalysisChartDataItem();

            item.setTitle(DateUtil.format(analysisByDay.getRecordDate(), "yyyy-MM-dd"));
            if (type == 101) {
                item.setAmount(analysisByDay.getNumRegistered().longValue());
            } else if (type == 102) {
                item.setAmount(analysisByDay.getNumActive().longValue());
            } else if (type == 103) {
                item.setAmount(analysisByDay.getNumRetention1d().longValue());
            }
            thisYearLsit.add(item);
        }
//        把今年的AnalysisChartDataItem集合，放到vo的thisYear里
        vo.setThisYear(thisYearLsit);

        //去年的数据
        //准备查询的日期范围
        String startDateLastYear = DateUtil.date(sd).offset(DateField.YEAR, -1).toString("yyyy-MM-dd");
        String endDateLastYear = DateUtil.date(ed).offset(DateField.YEAR, -1).toString("yyyy-MM-dd");
        //从数据库里查询日期范围内的数据
        analysisByDayList = query().between("record_date", startDateLastYear, endDateLastYear).list();
//        把每天的数据转换成AnalysisChartDataItem对象
        List<AnalysisChartDataItem> lastYearLsit = new ArrayList<>();
        for (AnalysisByDay analysisByDay : analysisByDayList) {
            AnalysisChartDataItem item = new AnalysisChartDataItem();

            item.setTitle(DateUtil.format(analysisByDay.getRecordDate(), "yyyy-MM-dd"));
            if (type == 101) {
                item.setAmount(analysisByDay.getNumRegistered().longValue());
            } else if (type == 102) {
                item.setAmount(analysisByDay.getNumActive().longValue());
            } else if (type == 103) {
                item.setAmount(analysisByDay.getNumRetention1d().longValue());
            }

            lastYearLsit.add(item);
        }
        //把去年的AnalysisChartDataItem集合，放到vo的thisYear里
        vo.setLastYear(lastYearLsit);
        return ResponseEntity.ok(vo);
    }


}
